<?php

use Attend as GlobalAttend;

defined('BASEPATH') or exit('No direct script access allowed');

class Attend extends WxHomeController
// class Attend extends MY_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->model("Admin_model");
        $this->load->model("Attend_model");
        $this->load->model("Attend_student_status_model");
        $this->load->model("Attend_teaching_content_model");
        $this->load->model("Attend_teaching_implementation_model");
        $this->load->model("Attend_img_model");
        $this->load->model('Student_feedback_model');
        $this->load->model('Supervisor_feedback_model');
        $this->load->model('Other_statistics_model');
        $this->load->model('Supervisor_feedback_record_model');
        $this->load->model('Student_feedback_record_model');
        $this -> load-> model('Table1_model');
        $this -> load-> model('Table2_model');
        $this -> load-> model('Table3_model');
        $this -> load-> model('Table4_model');
        $this -> load-> model('Table5_model');
        $this -> load-> model('Table6_model');
    }



    public function Index() {
        $this->load->view('home/supervision_mobile');
    }
    public function statistical() {
        $data['identity']=$this->identity($_SESSION[SESS_USER_HOME]);
        $this->load->view('home/statistical',$data);
    }
    public function lists() {
        $data['identity']=$this->identity($_SESSION[SESS_USER_HOME]);
        $this->load->view('home/list',$data);
    }
    public function feedback() {
        $data['identity']=$this->identity($_SESSION[SESS_USER_HOME]);
        $this->load->view('home/feedback',$data);
    }
    public function record() {
        $data['identity']=$this->identity($_SESSION[SESS_USER_HOME]);
        $this->load->view('home/record',$data);
    }
    public function details() {
        $this->load->view('home/details');
    }
    public function feedback_ist() {
        $data['identity']=$this->identity($_SESSION[SESS_USER_HOME]);
        $this->load->view('home/feedback_ist',$data);
    }
    public function other() {
        $this->load->view('home/other');
    }
    public function supervision() {
        $this->load->view('home/supervision');
    }
    public function feedback_inf() {
        $this->load->view('home/feedback_inf');
    }


    public function major() {
        $data['identity']=$this->identity($_SESSION[SESS_USER_HOME]);
        $this->load->view('home/major',$data);
    }


    /**
     * 方法名 attendList
     *
     * @Name attendList
     * @Desc 听课记录列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-22 09:50:06 星期四
     * @return      [type]                   [description]
     */
    public function attendList() {
        if (!isset($_POST['listen_id'], $_POST['pageNo'], $_POST['pageSize'])) {
            exit(retJson('参数错误', false));
        }

        $con = array(
            'where'    => $this->sqlLikeEscape("listen_id = ??", array($_POST['listen_id'])),
            'items'    => 'id, class_name, score, listen_type, type, created, teacher_code',
            'orderby'  => 'created desc',
            'pageNo'   => $_POST['pageNo'],
            'pageSize' => $_POST['pageSize'],
        );
        $list = $this->Attend_model->get_page_list($con);

        foreach ($list['ret'] as $key => $value) {
            $list['ret'][$key]['attendType'] = (new AttendType())->show($value['type']);
            $list['ret'][$key]['created']    = date('Y-m-d H:i:s', $value['created']);
        }

        // pri($list);exit();
        exit(retJson('请求成功', true, $list));
    }

    /**
     * 方法名 attendInfo
     *
     * @Name attendInfo
     * @Desc 听课记录详情
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-22 09:50:06 星期四
     * @return      [type]                   [description]
     */
    public function attendInfo() {
        if (!isset($_POST['aid'])) {
            exit(retJson('参数错误', false));
        }

        $con = array(
            'where' => $this->sqlLikeEscape("id = ??", array($_POST['aid'])),
            'items' => 'class_name, listen_address, listen_class, advice, created, score',
        );
        $info = $this->Attend_model->get_single_full($con);
        if (!isset($info)) {
            exit(retJson('当前记录不存在!', false));
        }


        // $student_status                 = $this->getInfo('get_score', $_POST['aid'], $this->Attend_student_status_model);
        // $teaching_content               = $this->getInfo('get_score', $_POST['aid'], $this->Attend_teaching_content_model);
        // $teaching_implementation        = $this->getInfo('get_score', $_POST['aid'], $this->Attend_teaching_implementation_model);
        // $img                                = $this->getInfo('img', $_POST['aid'], $this->Attend_img_model);
        // $info['student_status']          = $student_status['get_score'];
        // $info['teaching_content']        = $teaching_content['get_score'];
        // $info['teaching_implementation'] = $teaching_implementation['get_score'];
        $this->load->model('Attend_record_model');
        $info['record'] = $this->getInfo('commentary', $_POST['aid'], $this->Attend_record_model);
        $info['record']=$info['record']['commentary'];
        $img=$this->Attend_img_model->get_list(array('attend_id'=>$_POST['aid']));
        $img_list=array();
        foreach ($img as $key => $v) {
            $img_list[$v['type']][]=$v['img'];
        }
        $info['img']                     = $img_list;
        $info['created']                 = date('Y-m-d H:i:s', $info['created']);

        // pri($info);exit();
        exit(retJson('请求成功', true, $info));
    }

    private function getInfo($item, $attend_id, $model) {
        $con = array(
            'where' => $this->sqlLikeEscape("attend_id = ??", array($attend_id)),
            'items' => $item,
        );
        return $model->get_single_full($con);
    }


    // 获取身份
    private function identity($user) {
        $idtt = explode(',', $user['sub_role_id']);
        array_push($idtt, $user['role_id']);
        if (in_array(13, $idtt)) {
            // 管理员
            return $identity = 1;
        }
        if (in_array(5, $idtt)) {
            // 二级督导组长
            return $identity = 2;
        }
        if (in_array(6, $idtt) || in_array(4, $idtt) || in_array(2, $idtt)) {
            // 二级督导/校督导/教师
            return $identity = 3;
        }

        return 0;
    }



    /**
     * 方法名 coverteachingEvaluationList
     *
     * @Name 获取方法列和角色方法列
     * @Desc 课堂教学评价个人被听课列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-22 11:13:54 星期四
     * @return      [type]                   [description]
     */
    public function coverteachingEvaluationList() {
        if (!isset($_POST['admin_id'], $_POST['pageNo'], $_POST['pageSize'])) {
            exit(retJson('参数错误', false));
        }        
        $connar = array();
        $connar = $this->Attend_model->pickPages($connar, $_POST);
        $role=2;//具体详看后台
        $sub_role=explode(',', $_SESSION[SESS_USER_HOME]['sub_role_id']);
        array_push($sub_role, $_SESSION[SESS_USER_HOME]['role_id']);
       $sub_role= array_merge(array_filter($sub_role)); 
       $connar['items'] = "db_attend.*";
       $connar['where']=$this->sqlEscape('(db_attend.status=?? or db_attend.status=??)',array(AttendStatus::Submit[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value]));
       // $connar['orderby'] = "db_attend.id desc ";
       $connar['orderby'] = "back_time desc, db_attend.id desc ";
        $rold_id_arr = explode(",",$role);
            // 查询被听课数据            
            $connar['join']=array('db_admin ','db_admin.usercode = db_attend.teacher_code');
            $connar['where'].=$this->sqlEscape(' and db_attend.teacher_code=??',array($_SESSION[SESS_USER_HOME]['usercode']));
           
              //听课身份类型
            if(isset($_POST['type'])&& !empty($_POST['type']))
            {
                $connar['where'] .= $this->sqlEscape(" and db_attend.type=?? ",array($_POST['type']));
            }
            //听课类型
           if (isset($_POST['listen_type'])&& !empty($_POST['listen_type'])) 
            {
                $connar['where'] .= $this->sqlEscape(" and db_attend.listen_type=?? ",array($_POST['listen_type']));
            }

           if (isset($_POST['created']) && !empty($_POST['created'])) {
                $date = explode(",",$_POST['created']);
                  $connar['where'].=' and db_attend.listen_time>='.strtotime($date[0].' 00:00:00').' and db_attend.listen_time<='.strtotime($date[1].' 23:59:59');
            }

            $data = $this->Attend_model->get_page_list($connar);

            // echo $this->db->last_query();

            foreach ($data['ret'] as $k => $v) {
                $data['ret'][$k]['created'] = date('Y-m-d ', $data['ret'][$k]['listen_time']);
                 $data['ret'][$k]['attendType']=(new AttendType)->show($v['type']);
                 $data['ret'][$k]['status_char']=(new AttendStatus)->show($v['status']);            
                 if($v['back_time']){
                     $data['ret'][$k]['color']='red';
                     if($v['status']==AttendStatus::Submit[XPHEnum::Value]){
                         $data['ret'][$k]['status_char']='退回后提交';
                     }else{                    
                         $data['ret'][$k]['status_char']='退回中';
                     }
                 }
             }
        exit(retJson('请求成功', true, $data));
    }

    /**
     * 方法名 teachingEvaluationList
     *
     * @Name 获取方法列和角色方法列
     * @Desc 课堂教学评价去听课列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-22 11:13:54 星期四
     * @return      [type]                   [description]
     */
    public function teachingEvaluationList() {
        if (!isset($_POST['admin_id'], $_POST['pageNo'], $_POST['pageSize'])) {
            exit(retJson('参数错误', false));
        }

        // $connar = array(
        //     // 'where'    => $this->sqlLikeEscape("teacher_code = ??", array($_POST['teacher_code'])),
        //  //   'where'    => '1 = 1',
        //  //   'items'    => 'id,class_name, score, listen_type, type, created, advice, teacher_name, teacher_code, listen_name, course_units',
        //   //  'orderby'  => 'back_time desc,id desc',
        //     'pageNo'   => $_POST['pageNo'],
        //     'pageSize' => $_POST['pageSize'],
        // );
       

        // 身份筛选
        $user = $this->Admin_model->get_single(array('id' => $_POST['admin_id']));
        $sub_role = explode(',', $user['sub_role_id']);
        array_push($sub_role, $user['role_id']);

        // $identity = $this->identity($user);
        // $connar['where']=$this->sqlEscape("status=?? ", array(AttendStatus::Submit[XPHEnum::Value]));

        //不同时是校督导,又是督导组长
           if(!(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $sub_role) && in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $sub_role))){ 
             $connar = array();
            $connar = $this->Attend_model->pickPages($connar, $_POST);
            $connar['items'] = "db_attend.*,db_admin.major major,db_admin.dept_name listen_dept_name";
            $connar['join']=array('db_admin ','db_admin.id = db_attend.listen_id');
           /*  $connar['join'][1]=array('db_class','db_class.id = db_attend.class_id');
            $connar['join'][2]=array('db_attend_record','db_attend.id=db_attend_record.attend_id'); */
           $connar['where']=$this->sqlEscape('if(db_attend.listen_id=??,db_attend.status=?? or db_attend.status=??,if(back_time is null,db_attend.STATUS = ?? or db_attend.STATUS = ??, db_attend.STATUS = ?? or db_attend.STATUS = ??) )',array($user['id'],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
            $connar['orderby'] = "back_time desc, db_attend.id desc ";

            //管理员,,校领导,看到全校的听课数据
            if(in_array(WebRole::Manage[XPHEnum::Value], $sub_role) || in_array(WebRole::Admin[XPHEnum::Value], $sub_role)  || in_array(WebRole::SchoolLeader[XPHEnum::Value], $sub_role) || in_array(WebRole::SupervisorDirector[XPHEnum::Value], $sub_role)){
                $connar['where'].=" and 1=1";
            }else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $sub_role)){                
                //督导组长看到本分院的听课数据          
                if(in_array(WebRole::UnitLeader[XPHEnum::Value], $sub_role)){//同时是职能部门及二级教学单位负责人                  
                     if($user['dept_id']==123 || $user['dept_id']==107){ // 教务处/质量管理可以看到全校    
                       $connar['where'].=" and 1=1";
                     }else{
                        $connar['where']=$this->sqlEscape('if(db_attend.listen_id=??,db_attend.status=?? or db_attend.status=??,IF ( db_admin.role_id = ?? AND ( sub_role_id IS NULL OR sub_role_id = "" ), db_attend.STATUS =??  OR db_attend.STATUS =??, db_attend.STATUS = ?? ))',array($user['id'],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],WebRole::Teacher[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
                       $connar['where'].=$this->sqlEscape(' and (db_admin.dept_id=?? or db_attend.dept_name=??) and (db_attend.type=?? or db_attend.type=?? or db_attend.type=??)',array($user['dept_id'],str_replace('分', '学', $user['dept_name']),AttendType::Unit[XPHEnum::Value],AttendType::Outside[XPHEnum::Value],AttendType::Teacher[XPHEnum::Value]));

                     }
                }else{
                     $connar['where']=$this->sqlEscape('if(db_attend.listen_id=??,db_attend.status=?? or db_attend.status=??,IF ( db_admin.role_id = ?? AND ( sub_role_id IS NULL OR sub_role_id = "" ), db_attend.STATUS =??  OR db_attend.STATUS =??, db_attend.STATUS = ?? ))',array($user['id'],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],WebRole::Teacher[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
                       $connar['where'].=$this->sqlEscape(' and (db_admin.dept_id=?? or db_attend.dept_name=??) and (db_attend.type=?? or db_attend.type=?? or db_attend.type=??)',array($user['dept_id'],str_replace('分', '学', $user['dept_name']),AttendType::Unit[XPHEnum::Value],AttendType::Outside[XPHEnum::Value],AttendType::Teacher[XPHEnum::Value]));
                }
              
            }else if(in_array(WebRole::MajorUnitLeader[XPHEnum::Value], $sub_role)){
                 //专业负责人查看本专业的数据,不是专业负责人查看自己听课的数据
                 $connar['where'].=$this->sqlEscape(' and db_admin.major=??',array($user['major']));     
            }elseif(in_array(WebRole::UnitLeader[XPHEnum::Value], $sub_role)){//分院领导看自己部门的数据
                 // 教务处/质量管理可以看到全校,其他人看到本分院
                if($user['dept_id']!=123 && $user['dept_id']!=107){
                    $connar['where'].=$this->sqlEscape(' and db_admin.dept_id=??',array($user['dept_id']));
                }
            }else{//教师看到自己的听课数据
                  $connar['where'].=$this->sqlEscape(' and db_attend.listen_id=??',array($user['id']));    
            }

            //听课身份类型
            if(isset($_POST['type'])&& !empty($_POST['type']))
            {
                $connar['where'] .= $this->sqlEscape(" and db_attend.type=?? ",array($_POST['type']));
            }
            //听课类型
           if (isset($_POST['listen_type'])&& !empty($_POST['listen_type'])) 
            {
                $connar['where'] .= $this->sqlEscape(" and db_attend.listen_type=?? ",array($_POST['listen_type']));
            }

         if (isset($_POST['created']) && !empty($_POST['created'])) {
                $date = explode(",",$_POST['created']);
                  $connar['where'].=' and db_attend.listen_time>='.strtotime($date[0].' 00:00:00').' and db_attend.listen_time<='.strtotime($date[1].' 23:59:59');
            }

             //分院名称
             if(isset($_POST['course_units']) && !empty($_POST['course_units']))
            {
                 $connar['where'] .= " and db_attend.course_units like '%".$_POST['course_units']."%'";
                 // $connar['where'] .= $this->sqlEscape(" and (db_attend.dept_name=?? or db_attend.dept_name=??)",array(str_replace('分', '学', $_POST['dept_name']),str_replace('学', '分', $_POST['dept_name'])));
            }  

            $connar['where'] .= " and db_attend.class_name != '' and db_attend.class_type != ''  and  db_attend.type != '' and db_attend.course_units != ''  and db_attend.teacher_name != '' and db_attend.score != '0' and db_attend.teaching_level_score != '0' and db_attend.teaching_effect_score != '0' ";          


            $list =  $this->Attend_model->get_page_list($connar);
                    // echo $this->db->last_query();
           
        }else{//同时是校督导,督导组长

            if(in_array(WebRole::Manage[XPHEnum::Value], $sub_role)){
                // $where= $this->sqlEscape("where db_attend.status=??", array(AttendStatus::Submit[XPHEnum::Value]));
                $where =$this->sqlEscape(' where if(db_attend.listen_id=??,db_attend.status=?? or db_attend.status=??,if(back_time is null,db_attend.STATUS = ?? or db_attend.status=??, db_attend.STATUS = ?? or db_attend.STATUS = ??) )',array($user['id'],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
            }else{
                $where=$this->sqlEscape('where if(db_attend.listen_id=??,db_attend.status=?? or db_attend.status=??,IF ( db_admin.role_id = ?? AND ( sub_role_id IS NULL OR sub_role_id = "" ), db_attend.STATUS =??  OR db_attend.STATUS =??, if(back_time is null,db_attend.STATUS = ??, db_attend.STATUS = ?? or db_attend.STATUS = ??)))',array($user['id'],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],WebRole::Teacher[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value],AttendStatus::Save[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
                  if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $sub_role) && !in_array(WebRole::UnitLeader[XPHEnum::Value], $sub_role)){ 
                       $where .=$this->sqlEscape(' and (db_admin.dept_id=?? or db_attend.dept_name=??)',array($user['dept_id'],str_replace('分', '学', $user['dept_name'])));
                  }
                // $where.= $this->sqlEscape("  and dept_id=??", array($user['dept_id']));
            }

            if(in_array(WebRole::UnitLeader[XPHEnum::Value], $sub_role)){ //其他分院领导
                // 教务处/质量管理可以看到全校,其他人看到本分院
                if($user['dept_id']!=123 && $user['dept_id']!=107){
                      $where .=$this->sqlEscape(' and (db_admin.dept_id=?? or db_attend.dept_name=??)',array($user['dept_id'],str_replace('分', '学', $user['dept_name'])));
                }
            }

            //专业负责人查看本专业的数据
            if(in_array(WebRole::MajorUnitLeader[XPHEnum::Value], $sub_role)){
               $where .=$this->sqlEscape(' and db_admin.major=??',array($user['major']));
            }
            if (isset($_POST['class_name']) && $_POST['class_name'] != ''){           
                $where .= $this->sqlLikeEscape(" and db_attend.class_name like '%??%' ", array($_POST['class_name']));
            }
          
            //听课类型
           if (isset($_POST['type'])&& !empty($_POST['type'])) 
            {
                $where .= $this->sqlEscape(" and db_attend.type=?? ",array($_POST['type']));
            }
            
            //听课类型
            if(isset($_POST['listen_type'])&& !empty($_POST['listen_type']))
            {
                $where .= $this->sqlEscape(" and db_attend.listen_type=?? ",array($_POST['listen_type']));
            }

            if (isset($_POST['created']) && !empty($_POST['created'])) {
                $date = explode(",",$_POST['created']);
                $where.=' and db_attend.listen_time>='.strtotime($date[0].' 00:00:00').' and db_attend.listen_time<='.strtotime($date[1].' 23:59:59');
            } 
            
             //开课单位
             if(isset($_POST['course_units']) && !empty($_POST['course_units']))
            {
                 $where .= " and db_attend.course_units like '%".$_POST['course_units']."%'";
                // $connar['where'] .= $this->sqlEscape(" and (db_attend.dept_name=?? or db_attend.dept_name=??)",array(str_replace('分', '学', $_POST['dept_name']),str_replace('学', '分', $_POST['dept_name'])));              
            }


             $where .= " and db_attend.class_name != '' and db_attend.class_type != ''  and  db_attend.type != '' and db_attend.course_units != ''  and db_attend.teacher_name != '' and db_attend.score != '0' and db_attend.teaching_level_score != '0' and db_attend.teaching_effect_score != '0' ";

             $pageSize=$_POST['pageSize'];
            $pageNo=$_POST['pageNo']; 

            $sql=$this->sqlEscape("select `db_attend`.*,db_attend.id attend_id,db_admin.major,db_admin.dept_name listen_dept_name FROM `db_attend` LEFT JOIN `db_admin` ON `db_admin`.`id` = `db_attend`.`listen_id`".$where ." order by  back_time desc,db_attend.id desc  limit ".$pageSize* ($pageNo-1) .','.$pageSize,array());
            $sql1=$this->sqlEscape("select sum(count) as count from (select count(*) as count FROM `db_attend` LEFT JOIN `db_admin` ON `db_admin`.`id` = `db_attend`.`listen_id` ".$where ." ) count",array());    

            $list["ret"]=$this->Attend_model->query($sql);
            $count=$this->Attend_model->query($sql1);
            $list['count']=$count[0]['count'];
            $list['pageNo']=$pageNo;
            $list['totalPage']=ceil($list["count"] / $pageSize);

        }

        // if ($identity == 3) {
        //     // 二级督导/校督导/教师
        //     $con['where'].= $this->sqlLikeEscape(" and (teacher_code = ?? or listen_id = ??) ", array($user['usercode'], $user['id']));
        // } elseif ($identity == 2) {
        //     // 二级督导组长
        //     $c = array(
        //         'where' => $this->sqlLikeEscape(" dept_id = ?? and ( role_id in (5, 6) or FIND_IN_SET(5, sub_role_id) or FIND_IN_SET(6, sub_role_id)) ", array($user['dept_id'])),
        //         'items'  => 'id, role_id, sub_role_id',
        //     );
        //     $users = $this->Admin_model->get_list_full($c);
        //     $in = '';
        //     foreach ($users as $key => $value) {
        //         $in .= $value['id'] . ',';
        //     }

        //     $con['where'].= $this->sqlLikeEscape(" and (listen_id in (??) )", array(rtrim($in, ',')));
        // }


        // // 筛选
        // if (isset($_POST['created']) && !empty($_POST['created'])) {
        //     // $time = date('Y-m-d', strtotime($_POST['created']));
        //     // $start = strtotime($time);
        //     // $end   = strtotime('+1 day', $start);
        //     $date = explode(",",$_POST['created']);
        //     $start = strtotime($date[0].' 00:00:00');
        //     $end = strtotime($date[1].' 23:59:59');
        //     $con['where'] .= $this->sqlLikeEscape(" and created >=?? and created <=??", array($start, $end));
        // }
        // if ($identity != 2 && isset($_POST['course_units']) && !empty($_POST['course_units'])) {
        //     // 督导组长无法筛选
        //     $con['where'] .= $this->sqlLikeEscape(" and course_units = '??'", array($_POST['course_units']));
        // }
        // if (isset($_POST['listen_type'])&& !empty($_POST['listen_type'])) {
        //     $con['where'] .= $this->sqlLikeEscape(" and listen_type = '??'", array($_POST['listen_type']));
        // }
        // if (isset($_POST['type'])&& !empty($_POST['type'])) {
        //     $con['where'] .= $this->sqlLikeEscape(" and type = ??", array($_POST['type']));
        // }


        // $list = $this->Attend_model->get_page_list($con);
        foreach ($list['ret'] as $key => $value) {
            $list['ret'][$key]['attendType'] = (new AttendType())->show($value['type']);
            $list['ret'][$key]['created'] = date('Y-m-d H:i:s', $value['created']);
        }

        // echo $this->db->last_query();
        // pri($list);
        // exit();
        exit(retJson('请求成功', true, $list));
    }


    /**
     * 方法名 teachingEvaluationStatistics
     *
     * @Name teachingEvaluationStatistics
     * @Desc 课堂教学评价统计
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-22 11:13:54 星期四
     * @return      [type]                   [description]
     */
    public function teachingEvaluationStatistics() {
        if (!isset($_POST['admin_id'], $_POST['pageNo'], $_POST['pageSize'])) {
            exit(retJson('参数错误', false));
        }

        $con1 = array(
            'where'    => "teacher_code != '暂无工号' and status = ".AttendStatus::Submit[XPHEnum::Value],
            'items'    => 'id, teacher_code,course_units',
            'groupby'  => 'teacher_code',
            'orderby'  => 'listen_time desc',
            'pageNo'   => $_POST['pageNo'],
            'pageSize' => $_POST['pageSize'],
        );
        $where1 = '';
        // 身份筛选
        $user = $this->Admin_model->get_single(array('id' => $_POST['admin_id']));
        $identity = $this->identity($user);
        if ($identity == 3) {
            // 二级督导/校督导/教师
            exit(retJson('暂无数据', true, array()));
        } elseif ($identity == 2) {
            // 二级督导组长
            // $c = array(
            //     'where' => $this->sqlLikeEscape(" dept_id = ?? and ( role_id in (5, 6) or FIND_IN_SET(5, sub_role_id) or FIND_IN_SET(6, sub_role_id)) ", array($user['dept_id'])),
            //     'items'  => 'id, role_id, sub_role_id',
            // );
            // $users = $this->Admin_model->get_list_full($c);
            // $in = '';
            // foreach ($users as $key => $value) {
            //     $in .= $value['id'] . ',';
            // }

            // $con1['where'] = $this->sqlLikeEscape(" listen_id in (??) ", array(rtrim($in, ',')));
            if(str_replace('分', '学', $user['dept_name']) == '机电工程学院' || str_replace('分', '学',$user['dept_name']) == '机械工程学院'){
                $con1['where'].=$this->sqlEscape(' and (db_attend.course_units=?? or db_attend.course_units=??)',array('机电工程学院','机械工程学院'));
                $where1 .=$this->sqlEscape(' and (db_attend.course_units=?? or db_attend.course_units=??)',array('机电工程学院','机械工程学院'));
            }else{
                $con1['where'] .= $this->sqlLikeEscape(" and course_units = '??'", array(str_replace('分', '学', $user['dept_name'])));
                $where1 .= $this->sqlLikeEscape(" and course_units='??'", array(str_replace('分', '学', $user['dept_name'])));
            }
             
        }


        // 筛选
        if (isset($_POST['created']) && !empty($_POST['created'])) {
            // $time = date('Y-m-d', strtotime($_POST['created']));
            // $start = strtotime($time);
            // $end   = strtotime('+1 day', $start);
            $date = explode(",",$_POST['created']);
            $start = strtotime($date[0].' 00:00:00');
            $end = strtotime($date[1].' 23:59:59');
           $con1['where'] .= $this->sqlLikeEscape(" and listen_time >=?? and listen_time <=??", array($start, $end));
           $where1 .= $this->sqlLikeEscape(" and listen_time >=?? and listen_time <=??", array($start, $end));
        }
        if ($identity != 2 && isset($_POST['course_units']) && !empty($_POST['course_units'])) {
            // 督导组长无法筛选
            // $con1['where'] .= $this->sqlLikeEscape(" and course_units = '??'", array($_POST['course_units']));
            // $where1 .= $this->sqlLikeEscape(" and course_units='??'", array($_POST['course_units']));
              $con1['where'] .= " and course_units like '%".mb_substr($_POST['course_units'],0,2,"utf-8")."%'";
             $where1 .= " and course_units like '%".mb_substr($_POST['course_units'],0,2,"utf-8")."%'";
        }
        if (isset($_POST['listen_type']) && !empty($_POST['listen_type'])) {
            $con1['where'] .= $this->sqlLikeEscape(" and listen_type = '??'", array($_POST['listen_type']));
            $where1 .= $this->sqlLikeEscape(" and listen_type='??'", array($_POST['listen_type']));
        }
        if (isset($_POST['type']) && !empty($_POST['type'])) {
            $con1['where'] .= $this->sqlLikeEscape(" and type = ??", array($_POST['type']));
            $where1 .= $this->sqlLikeEscape(" and type='??'", array($_POST['type']));
        }
        $teachers = $this->Attend_model->get_list_full($con1);
        // echo $this->db->last_query();
        // pri($teachers);
        // exit();

        if (empty($teachers)) {
            exit(retJson('暂无数据', false));
        }

        // 总数
        $where = $con1['where'];
        $cot = "SELECT count(*) AS count FROM
                    (SELECT id
                    FROM `db_attend`
                    WHERE $where
                    GROUP BY `teacher_code`) AS t";
        $ret = $this->Attend_model->query($cot);
        $list['count'] = $ret[0]['count'];
        //总页数
        $list["totalPage"] = ceil($list["count"] / $_POST['pageSize']);
        $list["pageNo"] = $_POST['pageNo'];


        // 每个教师统计信息
        $con = array(
            'items'   => 'type, COUNT(*) AS count, CAST(AVG(score) AS DECIMAL(10,2)) AS avg_score',
            'groupby' => 'type',
            'orderby' => 'type asc',
        );
        foreach ($teachers as $key => $value) {
             $all = array(
                'items' => 'course_units,teacher_code, teacher_name, CAST(AVG(score) AS DECIMAL(10,2)) AS all_avg_score',
                'where' => $this->sqlLikeEscape("teacher_code = '??' and status=??", array($value['teacher_code'],AttendStatus::Submit[XPHEnum::Value])),
            );
             if($identity != 2){//督导组长无法筛选
                $all['where'] .= $where1;
             }
      
            $list['ret'][$key] = $this->Attend_model->get_single_full($all); // 总平均分
            $list['ret'][$key]['course_units'] = $value['course_units'];
          //  echo $this->db->last_query();echo "<br/>";

            $con['where'] = $this->sqlLikeEscape("teacher_code = '??' and status=??", array($value['teacher_code'],AttendStatus::Submit[XPHEnum::Value]));
            if($identity != 2){//督导组长无法筛选
                $con['where'] .= $where1;
           }
            $list['ret'][$key]['detail'] = $this->Attend_model->get_list_full($con);
           //  echo $this->db->last_query();echo "-----------<br/>";
            // pri( $list['ret'][$key]['detail']);echo "<br/>";
        }
     //   pri($list['ret']);echo "<br/>";
        // type转化
        foreach ($list['ret'] as $key => $value) {
            if($value['course_units']){
                foreach ($value['detail'] as $k => $v) {
                    if ($v['type'] == 1) {
                        $list['ret'][$key]['detail'][$k]['type_info'] = '校督导';
                    } elseif ($v['type'] == 2) {
                        $list['ret'][$key]['detail'][$k]['type_info'] = '二级督导';
                    } elseif ($v['type'] == 3) {
                        $list['ret'][$key]['detail'][$k]['type_info'] = '外聘督导';
                    }
                }
            }else{
                unset( $list['ret'][$key]);
            }
            
        }

        // pri($list);
        // exit();
        exit(retJson('请求成功', true, $list));
    }



    /**
     * 方法名 informationFeedback
     *
     * @Name informationFeedback
     * @Desc 信息反馈统计报表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-22 11:13:54 星期四
     * @return      [type]                   [description]
     */
    public function informationFeedback() {
        if (!isset($_POST['pageNo'], $_POST['pageSize'])) {
            exit(retJson('参数错误', false));
        }
        $pageNo = ($_POST['pageNo'] - 1) * $_POST['pageSize'];
        $pageSize = $_POST['pageSize'];
        
        $identity = $this->identity($_SESSION[SESS_USER_HOME]);
        $where=$this->sqlEscape(' where del_flag=?? and (feedback.status=?? or feedback.status=??) ',array(CommonStatus::UnDeleted[XPHEnum::Value],FeedBackStatus::HaveWrite[XPHEnum::Value],FeedBackStatus::End[XPHEnum::Value]));
        // $where=$this->sqlLikeEscape(" where del_flag=?? ", array(CommonWhether::Whether[XPHEnum::Value]));
        if ($identity == 3) {
            // 二级督导/校督导/教师
            $where.=" and 1<>1";
        } elseif ($identity == 2) {
            // 二级督导组长
            // $where.= $this->sqlEscape(" and feedback.dept_name = ?? ", array($_SESSION[SESS_USER_HOME]['dept_name']));    
            $where .= " and feedback.dept_name like '%".mb_substr($_SESSION[SESS_USER_HOME]['dept_name'],0,2,"utf-8")."%'";    
        }

        if (isset($_POST['created']) && !empty($_POST['created'])) {
            // $time = date('Y-m-d', strtotime($_POST['created']));
            // $start = strtotime($time);
            // $end   = strtotime('+1 day', $start);
            $date = explode(",",$_POST['created']);
            $start = strtotime($date[0].' 00:00:00');
            $end = strtotime($date[1].' 23:59:59');
            $where .= $this->sqlLikeEscape(" and feedback.created >=?? and feedback.created <= ??", array($start, $end));
        }
        if (isset($_POST['dept_name']) && !empty($_POST['dept_name'])) {
            // $where .= $this->sqlEscape(" and db_admin.dept_name=??", array($_POST['dept_name']));
              $where .=  " and db_admin.dept_name like '%".mb_substr($_POST['dept_name'],0,2,"utf-8")."%'";
        }


        //总数
        $cot = "SELECT count(*) AS count FROM (SELECT db_admin.id
                FROM
                (SELECT creator,del_flag,dept_name,created,status,role_belong FROM db_student_feedback UNION ALL SELECT creator,del_flag,dept_name,created,status,role_belong FROM db_supervisor_feedback ) feedback
                LEFT JOIN db_admin ON db_admin.id = feedback.creator".$where." GROUP by feedback.creator) AS cot";
        $ret = $this->Student_feedback_model->query($cot);
        $list['count'] = $ret[0]['count'];
        //总页数
        $list["totalPage"] = ceil($list["count"] / $pageSize);
        $list["pageNo"] = $_POST['pageNo'];

        // 数据
        $sql = "SELECT count(*) AS count,username,db_admin.dept_name,feedback.created AS created_time,creator
                FROM
                ( SELECT creator,created,del_flag,dept_name,status,role_belong FROM db_student_feedback UNION ALL SELECT creator,created,del_flag,dept_name,status,role_belong FROM db_supervisor_feedback ) feedback
                LEFT JOIN db_admin ON db_admin.id = feedback.creator
                ".$where."
                GROUP BY feedback.creator
                ORDER BY feedback.created DESC
                LIMIT $pageNo, $pageSize";
        $list['ret'] = $this->Student_feedback_model->query($sql);
        $list['all_count'] = array_sum(array_column($list['ret'], 'count'));

        // pri($list);exit();
        // echo $this->db->last_query();
        // exit();


        // $con = array(
        //     'items'    => 'ad.dept_name, count(*) as count, ad.username',
        //     'groupby'  => 'creator',
        //     'orderby'  => 'db_student_feedback.id asc',
        //     'join'     =>  array('db_admin ad', 'ad.id = db_student_feedback.creator'),
        //     'pageNo'   => $_POST['pageNo'],
        //     'pageSize' => $_POST['pageSize'],
        // );

        // $stu = $this->Student_feedback_model->get_list_full($con);
        // $con['join'] = array('db_admin ad', 'ad.id = db_supervisor_feedback.creator');
        // $con['orderby'] = 'db_supervisor_feedback.id asc';
        // $sup = $this->Supervisor_feedback_model->get_list_full($con);
        // $list = array_merge($stu, $sup);

        exit(retJson('请求成功', true, $list));
    }


    /**
     * 方法名 FeedbackList
     *
     * @Name FeedbackList
     * @Desc 信息反馈列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-22 11:13:54 星期四
     * @return      [type]                   [description]
     */
    public function feedbackList() {
        if (!isset($_POST['pageNo'], $_POST['pageSize'], $_POST['admin_id'])) {
            exit(retJson('参数错误', false));
        }

        // pri($_POST);exit();
        // 身份筛选
        $user = $this->Admin_model->get_single(array('id' => $_POST['admin_id']));
        $identity = $this->identity($user);
        if ($identity == 3) {
            // 二级督导/校督导/教师
            exit(retJson('暂无数据', true, array()));
        } elseif ($identity == 2) {
            // 二级督导组长
            $where1 = $this->sqlLikeEscape("db_supervisor_feedback.dept_name = '??' and del_flag='??'", array($user['dept_name'],CommonStatus::UnDeleted[XPHEnum::Value]));
            $where2 = $this->sqlLikeEscape("db_student_feedback.dept_name = '??'  and del_flag='??'", array($user['dept_name'],CommonStatus::UnDeleted[XPHEnum::Value]));
        } else {
            $where1 = '1 = 1 and del_flag='.CommonStatus::UnDeleted[XPHEnum::Value];
            $where2 = '1 = 1 and del_flag='.CommonStatus::UnDeleted[XPHEnum::Value];
        }

        // 筛选
        if (isset($_POST['created']) && !empty($_POST['created'])) {
            $time = date('Y-m-d', strtotime($_POST['created']));
            $start = strtotime($time);
            $end   = strtotime('+1 day', $start);
            $where1 .= $this->sqlLikeEscape(" and db_supervisor_feedback.created >=?? and db_supervisor_feedback.created <= ??", array($start, $end));
            $where2 .= $this->sqlLikeEscape(" and db_student_feedback.created >=?? and db_student_feedback.created <= ??", array($start, $end));
        }
        if (isset($_POST['creator']) && !empty($_POST['creator'])) {
            $where1 .= $this->sqlEscape(" and db_supervisor_feedback.creator=??", array($_POST['creator']));
            $where2 .= $this->sqlEscape(" and db_student_feedback.creator = ??", array($_POST['creator']));
        }
        if ($identity != 2 && isset($_POST['dept_name']) && !empty($_POST['dept_name'])) {
            // 督导组长无法筛选
            $where1 .= $this->sqlLikeEscape(" and db_supervisor_feedback.creator = '??'", array($_POST['dept_name']));
            $where2 .= $this->sqlLikeEscape(" and db_student_feedback.dept_name = '??'", array($_POST['dept_name']));
        }



        $pageNo = ($_POST['pageNo'] - 1) * $_POST['pageSize'];
        $pageSize = $_POST['pageSize'];
        //总数
        $cot = "SELECT count(*) AS count FROM
                    (SELECT `ad`.`dept_name`, `ad`.`username`, `category`, `content`, `db_supervisor_feedback`.`created`
                        FROM `db_supervisor_feedback`
                        LEFT JOIN `db_admin` `ad` ON `db_supervisor_feedback`.`creator` = `ad`.`id`
                        WHERE $where1
                    UNION ALL
                    SELECT `ad`.`dept_name`, `ad`.`username`, `category`, `content`, `db_student_feedback`.`created`
                        FROM `db_student_feedback`
                        LEFT JOIN `db_admin` `ad` ON `db_student_feedback`.`creator` = `ad`.`id`
                        WHERE $where2
                    ) AS feedback";
        $ret = $this->Student_feedback_model->query($cot);
        $list['count'] = $ret[0]['count'];

        //总页数
        $list["totalPage"] = ceil($list["count"] / $pageSize);
        $list["pageNo"] = $_POST['pageNo'];

        // 数据
        $sql = "SELECT * FROM
                    (SELECT `ad`.`dept_name`, `ad`.`username`, `category`, `content`, `db_supervisor_feedback`.`created`
                        FROM `db_supervisor_feedback`
                        LEFT JOIN `db_admin` `ad` ON `db_supervisor_feedback`.`creator` = `ad`.`id`
                        WHERE $where1
                    UNION ALL
                    SELECT `ad`.`dept_name`, `ad`.`username`, `category`, `content`, `db_student_feedback`.`created`
                        FROM `db_student_feedback`
                        LEFT JOIN `db_admin` `ad` ON `db_student_feedback`.`creator` = `ad`.`id`
                        WHERE $where2
                    ) AS feedback
                ORDER BY feedback.created DESC
                LIMIT $pageNo, $pageSize";
        $list['ret'] = $this->Student_feedback_model->query($sql);
        // echo $this->db->last_query();



        foreach ($list['ret'] as $key => $value) {
            $list['ret'][$key]['created'] = date('Y-m-d H:i:s', $value['created']);
        }
        // pri($list);
        // exit();
        exit(retJson('请求成功', true, $list));
    }



    /**
     * 方法名 otherStatistics
     *
     * @Name otherStatistics
     * @Desc 其他材料统计报表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-26 15:47:59 星期一
     * @return      [type]                   [description]
     */
    public function otherStatistics() {
        if (!isset($_POST['pageNo'], $_POST['pageSize'], $_POST['admin_id'])) {
            exit(retJson('参数错误', false));
        }

        if (isset($_POST['created']) && !empty($_POST['created'])) {
            $date = explode(",",$_POST['created']);
            $start = strtotime($date[0].' 00:00:00');
            $end = strtotime($date[1].' 23:59:59');
         }
            $connar = array();
            $params = $_POST;
             $connar = $this -> Admin_model -> pickPages($connar, $params);
            $connar['items']="DISTINCT (db_admin.id),usercode,username,dept_name";
            $connar['join'][0]=array('db_table1','db_table1.admin_id=db_admin.id');
            $connar['join'][1]=array('db_table2','db_table2.admin_id=db_admin.id');
            $connar['join'][2]=array('db_table3','db_table3.admin_id=db_admin.id');
            $connar['join'][3]=array('db_table4','db_table4.admin_id=db_admin.id');
            $connar['join'][4]=array('db_table5','db_table5.admin_id=db_admin.id');
            $connar['join'][5]=array('db_table6','db_table6.admin_id=db_admin.id');
            $connar['where']=$this->sqlEscape('db_admin.status<>?? and (db_table1.admin_id IS NOT NULL OR db_table2.admin_id IS NOT NULL OR db_table3.admin_id IS NOT NULL OR db_table4.admin_id IS NOT NULL  OR db_table5.admin_id IS NOT NULL)',array(CommonStatus::Deleted[XPHEnum::Value]));
             $user = $this->Admin_model->get_single(array('id' => $_POST['admin_id']));
            $role=explode(',',$user['sub_role_id']);
            array_push($role,$user['role_id']);
            
            if(in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::SchoolLeader[XPHEnum::Value], $role) ){
              $connar['where'].=' and 1=1 ';
            }else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
              $connar['where'].=$this->sqlEscape(' and db_admin.dept_name=?? ',array($_SESSION[SESS_USER_HOME]['dept_name']));
            }else if(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::ExternalSupervisor[XPHEnum::Value], $role)||in_array(WebRole::SupervisorDirector[XPHEnum::Value], $role)){
              $connar['where'].=$this->sqlEscape(' and db_admin.id=?? ',array($_SESSION[SESS_USER_HOME]['id']));
            }

            $list = $this -> Admin_model -> get_page_list($connar);
              // 重新计算条数
            $count=$this->Admin_model->query("SELECT count(*) count from (SELECT DISTINCT   db_admin.`usercode`,db_admin.`id`,  db_admin.`username` FROM `db_admin` LEFT JOIN db_table1 ON db_admin.id = db_table1.admin_id LEFT JOIN db_table2 ON db_admin.id = db_table2.admin_id LEFT JOIN db_table3 ON db_admin.id = db_table3.admin_id LEFT JOIN db_table4 ON db_admin.id = db_table4.admin_id LEFT JOIN db_table5 ON db_admin.id = db_table5.admin_id  LEFT JOIN db_table6 ON db_admin.id = db_table6.admin_id WHERE ".$connar['where'].")a");
            $list['count']=$count[0]['count'];
            $list["totalPage"] = ceil($list["count"] / $params['pageSize']);

            $adminlist = array();
            foreach ($list['ret'] as $k => $v) {  

              // 教学督导专题教研次数
              $connar1['where']=$this->sqlEscape('admin_id=??',array($v['id']));
              $connar1['items']='count(*) table1Time';
              if (isset($_POST['created']) && !empty($_POST['created'])) {
                $connar1['where'].=$this ->sqlEscape(' and created>?? and created<??',array($start,$end));
              }
              $table1Time=$this-> Table1_model->get_list_full($connar1);

              $list['ret'][$k]['specialResearch']=$table1Time[0]['table1Time'];
              // 督导教学巡(检)查情况记录表次数
              $connar2['where']=$this->sqlEscape('admin_id=??',array($v['id']));
              $connar2['items']='count(*) table2Time';
              if (isset($_POST['created']) && !empty($_POST['created'])) {
                $connar2['where'].=$this ->sqlEscape(' and created>?? and created<??',array($start,$end));
              }
              $table2Time=$this-> Table2_model->get_list_full($connar2);

            

              $list['ret'][$k]['patrol']=$table2Time[0]['table2Time'];
              // 督导巡考情况记录表次数
              $connar3['where']=$this->sqlEscape('admin_id=??',array($v['id']));
              $connar3['items']='count(*) table3Time';
              if (isset($_POST['created']) && !empty($_POST['created'])) {
                $connar3['where'].=$this ->sqlEscape(' and created>?? and created<??',array($start,$end));
              }
              $table3Time=$this-> Table3_model->get_list_full($connar3);
              $list['ret'][$k]['tourInformant']=$table3Time[0]['table3Time'];
              // 教学状态专项检查表次数
              $connar4['where']=$this->sqlEscape('admin_id=??',array($v['id']));
              $connar4['items']='count(*) table4Time';
              if (isset($_POST['created']) && !empty($_POST['created'])) {
                $connar4['where'].=$this ->sqlEscape(' and created>?? and created<??',array($start,$end));
              }
              $table4Time=$this-> Table4_model->get_list_full($connar4);
              $list['ret'][$k]['specialInspection']=$table4Time[0]['table4Time'];
              // 学生顶岗实习巡查次数
              $connar5['where']=$this->sqlEscape('admin_id=??',array($v['id']));
              $connar5['items']='count(*) table5Time';
              if (isset($_POST['created']) && !empty($_POST['created'])) {
                $connar5['where'].=$this ->sqlEscape(' and created>?? and created<??',array($start,$end));
              }
              $table5Time=$this-> Table5_model->get_list_full($connar5);
              $list['ret'][$k]['postPractice']=$table5Time[0]['table5Time'];
              // 教学信息员专题调查表次数
              $connar6['where']=$this->sqlEscape('admin_id=??',array($v['id']));
              $connar6['items']='count(*) table6Time';
              if (isset($_POST['created']) && !empty($_POST['created'])) {
                $connar6['where'].=$this ->sqlEscape(' and created>?? and created<??',array($start,$end));
              }
              $table6Time=$this-> Table6_model->get_list_full($connar6);
              $list['ret'][$k]['messenger']=$table6Time[0]['table6Time'];
            } 

            // pri($list['ret']); 
            //  $list['all_count'] = array(
            //     'specialResearch'   => array_sum(array_column($list['ret'], 'specialResearch')),
            //     'patrol'            => array_sum(array_column($list['ret'], 'patrol')),
            //     'tourInformant'     => array_sum(array_column($list['ret'], 'tourInformant')),
            //     'specialInspection' => array_sum(array_column($list['ret'], 'specialInspection')),
            //     'postPractice'      => array_sum(array_column($list['ret'], 'postPractice')),
            //     'messenger'         => array_sum(array_column($list['ret'], 'messenger')),
            // );

             // pri($list['all_count']);

            // $con = array(
            //     'where'    => '1 = 1',
            //     'items'    => 'username, dept_name, specialResearch, patrol, tourInformant, specialInspection, postPractice, messenger',
            //     'pageNo'   => $_POST['pageNo'],
            //     'pageSize' => $_POST['pageSize'],
            // );

            // // 身份筛选
            // $user = $this->Admin_model->get_single(array('id' => $_POST['admin_id']));
            // $identity = $this->identity($user);
            // if ($identity == 3) {
            //     // 二级督导/校督导/教师
            //     exit(retJson('暂无数据', true, array()));
            // } elseif ($identity == 2) {
            //     $con['where'] = $this->sqlLikeEscape("dept_name = '??'", array($user['dept_name']));
            // }

            // $list = $this->Other_statistics_model->get_page_list($con);
            // $list['all_count'] = array(
            //     'specialResearch'   => array_sum(array_column($list['ret'], 'specialResearch')),
            //     'patrol'            => array_sum(array_column($list['ret'], 'patrol')),
            //     'tourInformant'     => array_sum(array_column($list['ret'], 'tourInformant')),
            //     'specialInspection' => array_sum(array_column($list['ret'], 'specialInspection')),
            //     'postPractice'      => array_sum(array_column($list['ret'], 'postPractice')),
            //     'messenger'         => array_sum(array_column($list['ret'], 'messenger')),
            // );
        
        // pri($list);
        // exit();
        exit(retJson('请求成功', true, $list));
    }

    // public function otherStatistics1()
    // {
    //     if (!isset($_POST['pageNo'], $_POST['pageSize'], $_POST['admin_id'])) {
    //         exit(retJson('参数错误', false));
    //     }

    //     $con = array(
    //         'where'    => '1 = 1',
    //         'items'    => 'username, dept_name, count(*) as count, admin_id',
    //         'groupby'  => 'admin_id',
    //         // 'pageNo'   => $_POST['pageNo'],
    //         // 'pageSize' => $_POST['pageSize'],
    //     );


    //     // 身份筛选
    //     $user = $this->Admin_model->get_single(array('id' => $_POST['admin_id']));
    //     $identity = $this->identity($user);
    //     if ($identity == 3) {
    //         // 二级督导/校督导/教师
    //         exit(retJson('暂无数据', true, array()));
    //     } elseif ($identity == 2) {
    //         $con['where'] = $this->sqlLikeEscape("a.dept_name = '??'", array($user['dept_name']));
    //     }


    //     $con['join'] = array('db_admin as a', 'db_table1.admin_id = a.id');
    //     $specialResearch = $this->Table1_model->get_list_full($con);

    //     $con['join'] = array('db_admin as a', 'db_table2.admin_id = a.id');
    //     $patrol = $this->Table2_model->get_list_full($con);

    //     $con['join'] = array('db_admin as a', 'db_table3.admin_id = a.id');
    //     $tourInformant = $this->Table3_model->get_list_full($con);

    //     $con['join'] = array('db_admin as a', 'db_table4.admin_id = a.id');
    //     $specialInspection = $this->Table4_model->get_list_full($con);

    //     $con['join'] = array('db_admin as a', 'db_table5.admin_id = a.id');
    //     $postPractice = $this->Table5_model->get_list_full($con);

    //     $con['join'] = array('db_admin as a', 'db_table6.admin_id = a.id');
    //     $messenger = $this->Table6_model->get_list_full($con);

    //     // 用户名作为键名
    //     $a = array_column($specialResearch, null, 'username');
    //     $b = array_column($patrol, null, 'username');
    //     $c = array_column($tourInformant, null, 'username');
    //     $d = array_column($specialInspection, null, 'username');
    //     $e = array_column($postPractice, null, 'username');
    //     $f = array_column($messenger, null, 'username');

    //     // pri($c);
    //     // exit();
    //     $users = array_column($tourInformant, 'username');
    //     foreach ($users as $key => $value) {
    //         $list[$key] = array(
    //             'username'          => $value,
    //             'admin_id'          => $c[$value]['admin_id'],
    //             'dept_name'         => $c[$value]['dept_name'],
    //             'specialResearch'   => array_key_exists($value, $a) ? $a[$value]['count'] : 0 ,
    //             'Patrol'            => array_key_exists($value, $b) ? $b[$value]['count'] : 0 ,
    //             'tourInformant'     => array_key_exists($value, $c) ? $c[$value]['count'] : 0 ,
    //             'specialInspection' => array_key_exists($value, $d) ? $d[$value]['count'] : 0 ,
    //             'postPractice'      => array_key_exists($value, $e) ? $e[$value]['count'] : 0 ,
    //             'messenger'         => array_key_exists($value, $f) ? $f[$value]['count'] : 0 ,
    //         );
    //     }

    //     $this->Other_statistics_model->add_batch($list);

    //     pri($list);
    //     exit();
    //     exit(retJson('请求成功', true, $list));
    // }



    /**
     * 方法名 teacherFeedBack
     *
     * @Name teacherFeedBack
     * @Desc 信息反馈（教师查看反馈给自己的内容）
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-07-26 15:47:59 星期一
     * @return      [type]                   [description]
     */
    public function teacherFeedBack() {
        if (!isset($_POST['admin_id'], $_POST['pageNo'], $_POST['pageSize'])) {
            exit(retJson('参数错误', false));
        }


        $admin_id = $_POST['admin_id'];
        $pageNo = ($_POST['pageNo'] - 1) * $_POST['pageSize'];
        $pageSize = $_POST['pageSize'];

        // 总数
        $cot = "SELECT count(*) as count FROM
                (
                    SELECT stu.created,stu.content FROM db_student_feedback_record LEFT JOIN db_student_feedback AS stu
                        ON db_student_feedback_record.feedback_id = stu.id WHERE admin_id = $admin_id
                    UNION ALL
                    SELECT sup.created,sup.content FROM db_supervisor_feedback_record LEFT JOIN db_supervisor_feedback AS sup
                        ON db_supervisor_feedback_record.feedback_id = sup.id WHERE admin_id = $admin_id
                ) AS record";
        $ret = $this->Student_feedback_record_model->query($cot);
        $list['count'] = $ret[0]['count'];

        //总页数
        $list["totalPage"] = ceil($list["count"] / $pageSize);
        $list["pageNo"] = $_POST['pageNo'];


        // 数据
        $sql = "SELECT * FROM
                (
                    SELECT stu.created,stu.content FROM db_student_feedback_record LEFT JOIN db_student_feedback AS stu
                        ON db_student_feedback_record.feedback_id = stu.id WHERE admin_id = $admin_id
                    UNION ALL
                    SELECT sup.created,sup.content FROM db_supervisor_feedback_record LEFT JOIN db_supervisor_feedback AS sup
                        ON db_supervisor_feedback_record.feedback_id = sup.id WHERE admin_id = $admin_id
                ) AS record
                ORDER BY record.created DESC
                LIMIT $pageNo, $pageSize";
        $ret = $this->Student_feedback_record_model->query($sql);
        foreach ($ret as $key => $value) {
            $ret[$key]['created'] = date('Y-m-d h:i:s', $value['created']);
        }
        $list['ret'] = $ret;

        // echo $this->db->last_query();
        // pri($list);exit();
        exit(retJson('请求成功', true, $list));
    }



    public function teachingScreen() {
        $sql = "SELECT listen_type FROM db_attend WHERE listen_type IS NOT NULL GROUP BY listen_type ";
        $res1 = $this->Attend_model->query($sql);
        $screen['listen_type'] = array_column($res1, 'listen_type');

        //开课单位
        // $sql = "SELECT course_units FROM db_attend WHERE course_units IS NOT NULL and course_units != '' and status > ".AttendStatus::Save[XPHEnum::Value]." GROUP BY course_units ";
        // $res2 = $this->Attend_model->query($sql);
        // $screen['course_units'] = array_column($res2, 'course_units');
        //   $new_dept_name=array();
        // foreach($screen['course_units'] as $k=>$v){
        //     if($v){
        //         $name = mb_substr($v,0,2,"utf-8");             
        //         if($name =='财经' ||$name =='商贸'||$name =='交通'||$name =='鉴湖'||$name =='黄酒'){
        //            $new_dept_name[$k]= mb_substr($v,0,2,"utf-8").'学院';             
        //         }else if($name =='建筑' ||$name =='机电'||$name =='交通'||$name =='机械'){
        //            $new_dept_name[$k]= mb_substr($v,0,4,"utf-8").'学院';   
        //         }else if($name =='设计'){
        //             $new_dept_name[$k]= mb_substr($v,0,5,"utf-8").'学院';   
        //         }elseif($name == '电气'){
        //            $new_dept_name[$k]= mb_substr($v,0,6,"utf-8").'学院';   
        //         }else{
        //            $new_dept_name[$k] = $v;
        //         }
        //     }
        // }
        // $screen['course_units'] = array_merge(array_unique($new_dept_name));

             //获取开课单位
         $screen['course_units'] = (new AdminDeptList)->getRemark()[1]; 

        $screen['type'] = array_column((new AttendType)->getAll(), 1);
        exit(retJson('请求成功', true, $screen));
    }

    public function feedbackScreen() {
       //  $sql = "SELECT dept_name FROM db_student_feedback WHERE dept_name IS NOT NULL GROUP BY dept_name ";
       //  $res1 = $this->Student_feedback_record_model->query($sql);
       //  $sql = "SELECT dept_name FROM db_supervisor_feedback WHERE dept_name IS NOT NULL GROUP BY dept_name ";
       //  $res2 = $this->Supervisor_feedback_record_model->query($sql);
       //  $screen['dept_name'] = array_keys(array_flip(array_column($res1, 'dept_name')) + array_flip(array_column($res2, 'dept_name')));
       //   $new_dept_name=array();
       //  foreach($screen['dept_name'] as $k=>$v){
       //      if($v){
       //          $name = mb_substr($v,0,2,"utf-8");             
       //          if($name =='财经' ||$name =='商贸'||$name =='交通'||$name =='鉴湖'||$name =='黄酒'){
       //             $new_dept_name[$k]= mb_substr($v,0,2,"utf-8").'学院';             
       //          }else if($name =='建筑' ||$name =='机电'||$name =='交通'||$name =='机械'){
       //             $new_dept_name[$k]= mb_substr($v,0,4,"utf-8").'学院';   
       //          }else if($name =='设计'){
       //              $new_dept_name[$k]= mb_substr($v,0,5,"utf-8").'学院';   
       //          }elseif($name == '电气'){
       //             $new_dept_name[$k]= mb_substr($v,0,6,"utf-8").'学院';   
       //          }else{
       //             $new_dept_name[$k] = $v;
       //          }
       //      }
       //  }
       // $screen['dept_name'] = array_merge(array_unique($new_dept_name));

      //获取开课单位
         $screen['dept_name'] = (new AdminDeptList)->getRemark()[1]; 
        // pri( $screen['dept_name']);

        exit(retJson('请求成功', true, $screen));
    }


    /**
     * []
     * @Name     名称
     * @Desc     专业负责人听课统计
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author   xy
     * @DateTime 2022-01-20T09:57:03+0800
     * @return   [type]                   [description]
     */
    public function majorTeachingEvaluationList(){
         if (!isset($_POST['admin_id'], $_POST['pageNo'], $_POST['pageSize'])) {
            exit(retJson('参数错误', false));
        }       

         $pageSize=$_POST['pageSize'];
         $pageNo=$_POST['pageNo']; 

       // 身份筛选
        $user = $this->Admin_model->get_single(array('id' => $_POST['admin_id']));
        $role=2;//具体详看后台
        $sub_role=explode(',', $user['sub_role_id']);
        array_push($sub_role, $user['role_id']);
       $sub_role= array_merge(array_filter($sub_role)); 

       $where = "(db_attend.status=".AttendStatus::Submit[XPHEnum::Value]." or db_attend.status=".AttendStatus::Save[XPHEnum::Value].")";

       $where .= " and db_admin.major='".$user['major']."'"; 

          //开课单位名称
         if(isset($_POST['course_units']) && !empty($_POST['course_units']))
        {
             $where .= " and db_attend.course_units like '%".$_POST['course_units']."%'";
        }  

        //开始时间,结束时间
        if (isset($_POST['created']) && !empty($_POST['created'])) {
            $date = explode(",",$_POST['created']);
            $where.=' and db_attend.listen_time>='.strtotime($date[0].' 00:00:00').' and db_attend.listen_time<='.strtotime($date[1].' 23:59:59');
        }


       $item = "db_attend.id,db_attend.listen_id,db_admin.usercode listen_code,db_attend.listen_name,db_attend.teacher_code,db_attend.teacher_name,
        db_attend.dept_name, db_attend.class_name,db_attend.course_units,db_attend.listen_time,totalScore.avg_total_score,db_admin.major major,db_admin.dept_name listen_dept_name ";

        $leftjoin1 = " left join db_admin on db_admin.id = db_attend.listen_id";
        $leftjoin2 = " left join (SELECT CAST(avg(score) AS DECIMAL(10,2)) avg_total_score,listen_id,listen_name,teacher_code,teacher_name from db_attend LEFT JOIN db_admin ON db_admin.id = db_attend.listen_id 
             where ".$where." GROUP BY teacher_code )totalScore on totalScore.teacher_code = db_attend.teacher_code";

        $groupby = "teacher_code";
        $orderby = "listen_name desc,listen_time desc";


        //查询语句
       $sql = "select ".$item." from db_attend ".$leftjoin1.$leftjoin2." where ".$where." group by teacher_code order by ".$orderby." limit ".$pageSize* ($pageNo-1) .','.$pageSize;
       $sql1 = "select ".$item." from db_attend ".$leftjoin1.$leftjoin2." where ".$where." group by teacher_code order by ".$orderby;
        $data["ret"]=$this->Attend_model->query($sql);
        $count=$this->Attend_model->query($sql1);
        $data['count']=count($count);
        $data['pageNo']=$pageNo;
        $data['totalPage']=ceil($data["count"] / $pageSize);

            foreach ($data['ret'] as $k => $v) {
                $data['ret'][$k]['created'] = date('Y-m-d ', $data['ret'][$k]['listen_time']);
             }
        exit(retJson('请求成功', true, $data));

    }
}
